package com.blt.car.me;

import android.view.View;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;
import com.blt.car.utils.ActivityHelper;
import com.blt.car.widget.dialog.BalanceDialog;

import java.text.DecimalFormat;

import blt.app_core.api.base.CoreEvent;
import blt.app_core.api.base.CoreManager;
import blt.app_core.api.purse.PayApi;
import blt.app_core.api.purse.bean.PurseVo;

/**
 * Created by xgp on 2017/6/20.
 */

public class MyPurseActivity extends BaseTitleBarActivity implements View.OnClickListener {
    private TextView tvBalance;
    private TextView tvCouponNums;
    private TextView tvRedNums;
    private TextView tvCreditsNums;
    private PurseVo purseVo;

    private String redAmount;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_purse;
    }

    @Override
    protected void initData() {
        super.initData();
        CoreManager.getCore(PayApi.class).getPurseInfo();

    }

    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("我的钱包");
        mTitleBar.setRightText("明细");
        mTitleBar.getRightTextView().setTextColor(getResources().getColor(R.color.text_color_black));
        mTitleBar.getRightTextView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(RoadCostDetailActivity.class);
            }
        });

        holder.get(R.id.my_purse_my_red);
        holder.get(R.id.my_purse_my_coupon);
        holder.get(R.id.my_purse_optical);
        holder.get(R.id.purse_voucher);
        tvCreditsNums = holder.get(R.id.my_purse_credits_nums);
        tvRedNums = holder.get(R.id.my_purse_red_nums);
        tvCouponNums = holder.get(R.id.my_purse_my_coupon_nums);
        tvBalance = holder.get(R.id.my_purse_money);
        holder.setOnClickListener(this, R.id.purse_voucher, R.id.my_purse_my_red, R.id.my_purse_my_coupon,
                R.id.my_purse_optical, R.id.my_purse_money);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.purse_voucher:
                long balance = (purseVo.chargeAmt + purseVo.giveAmt) / 100;
//                ActivityHelper.toChargeActivity(this,balance);
                openActivity(ChargeActivity.class, ChargeActivity.CHARGE_AMOUNT, balance);
                break;
            case R.id.my_purse_my_red:
                openActivity(MyRedActivity.class, MyRedActivity.RED_AMT,redAmount);
                break;
            case R.id.my_purse_my_coupon:
                openActivity(MyCouponActivity.class, MyCouponActivity.COUPON_NUMS, purseVo);
                break;
            case R.id.my_purse_optical:
                openActivity(MyIntegralActivity.class);
                break;
            case R.id.my_purse_money:
                BalanceDialog balanceDialog = new BalanceDialog(this, purseVo);
                balanceDialog.show();
                break;
        }
    }


    @CoreEvent(coreClientClass = PayApi.class)
    public void onPurseInfo(PurseVo purseVo) {
        this.purseVo = purseVo;
        redAmount=purseVo.redPackageAmt;
        long balance = (purseVo.chargeAmt + purseVo.giveAmt) / 100;
        tvBalance.setText(getResources().getString(R.string.my_balance, balance));
        tvCouponNums.setText(getResources().getString(R.string.coupon_nums, purseVo.couponCount));
        tvCreditsNums.setText(getResources().getString(R.string.credits_nums, purseVo.bonusPoints));
        tvRedNums.setText(getResources().getString(R.string.red_package_nums, purseVo.redPackageAmt));
        if(Float.valueOf(purseVo.redPackageAmt)<=0){
            tvRedNums.setText("０元");
        }
    }

    @CoreEvent(coreClientClass = PayApi.class)
    public void onExtract(String amount) {
        toastShort("提现成功");
        float newAmount=Float.valueOf(purseVo.redPackageAmt)-Float.valueOf(amount);
        DecimalFormat de=new DecimalFormat("0.00");
        redAmount=de.format(newAmount);
        tvRedNums.setText(getResources().getString(R.string.red_package_nums, redAmount));
    }

    @CoreEvent(coreClientClass = PayApi.class)
    public void onExtract() {
        tvRedNums.setText(getResources().getString(R.string.red_package_nums, "0"));
    }

    @CoreEvent(coreClientClass = PayApi.class)
    public void onChargeSuccess(long amount){
        tvBalance.setText(getResources().getString(R.string.my_balance, amount));

    }

}
